package com.itjason.controller;

import com.itjason.dto.UserLoginDataDTO;
import com.itjason.exceptionhandler.EnumException;
import com.itjason.exceptionhandler.ResponseException;
import com.itjason.service.WxUserService;
import com.itjason.vo.UserInfoVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * Author:JasonLong
 * Date:2023/5/9
 * 用户业务控制类
 */
@RestController
@RequestMapping("/wx/user")
public class WxUserController {

    @Autowired
    private WxUserService wxUserService;

    //后端测试接口(用于测试是否成功与后端建立连接)
    @RequestMapping("/testApi")
    public String testapi(@RequestParam String code) {
        return "成功与后端建立连接！收到js_code:" + code;
    }

    /**
     * 登陆接口
     *
     * @Params loginDataDTO
     * @Return token
     * 该参数是wx.login登陆成功后返回的
     * 该接口本应该给小程序返回一个token
     * 但是测试阶段暂时不做--后期需要直接将返回值成需要的即可
     */
    @ApiOperation(value = "用户登录接口")
    @ApiImplicitParam(name = "UserLoginDataDTO", value = "UserLoginDataDTO,实体类对象参数", dataType = "UserLoginDataDTO",paramType = "body")
    @PostMapping("/login")
    public ResponseException login(@RequestBody UserLoginDataDTO userLoginDataDTO) {
        if (StringUtils.isBlank(userLoginDataDTO.getCode()) || StringUtils.isBlank(userLoginDataDTO.getEncryptedData())
                || StringUtils.isBlank(userLoginDataDTO.getIv())) {
            return ResponseException.error(EnumException.BAD_REQUEST.getResultCode(), EnumException.BAD_REQUEST.getResultMsg());
        }
        return wxUserService.saveLoginInfo(userLoginDataDTO);
    }

    @ApiOperation(value = "用户信息获取接口")
    @GetMapping("userInfo")
    public UserInfoVO getUserInfo(@RequestParam(value = "userToken",required = false) String userToken){

        return wxUserService.getUserInfo(userToken);
    }
}
